1   // Copyright 2011 The Apache Software Foundation
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   //     http://www.apache.org/licenses/LICENSE-2.0
8   //
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  
15  package org.apache.tapestry5.internal.alerts;
16  
17  import org.apache.tapestry5.alerts.*;
18  import org.apache.tapestry5.ioc.services.PerThreadValue;
19  import org.apache.tapestry5.ioc.services.PerthreadManager;
20  import org.apache.tapestry5.services.ApplicationStateManager;
21  import org.apache.tapestry5.services.Request;
22  import org.apache.tapestry5.services.ajax.AjaxResponseRenderer;
23  import org.apache.tapestry5.services.ajax.JavaScriptCallback;
24  import org.apache.tapestry5.services.javascript.JavaScriptSupport;
25  
26  public class AlertManagerImpl implements AlertManager
27  {
28      private final ApplicationStateManager asm;
29  
30      private final Request request;
31  
32      private final AjaxResponseRenderer ajaxResponseRenderer;
33  
34      private final PerThreadValue<Boolean> needAlertStorageCleanup;
35  
36      public AlertManagerImpl(ApplicationStateManager asm, Request request, AjaxResponseRenderer ajaxResponseRenderer, PerthreadManager perThreadManager)
37      {
38          this.asm = asm;
39          this.request = request;
40          this.ajaxResponseRenderer = ajaxResponseRenderer;
41  
42          needAlertStorageCleanup = perThreadManager.createValue();
43      }
44  
45      public void success(String message)
46      {
47          alert(Duration.SINGLE, Severity.SUCCESS, message);
48      }
49      
50      public void info(String message)
51      {
52          alert(Duration.SINGLE, Severity.INFO, message);
53      }
54  
55      public void warn(String message)
56      {
57          alert(Duration.SINGLE, Severity.WARN, message);
58      }
59  
60      public void error(String message)
61      {
62          alert(Duration.SINGLE, Severity.ERROR, message);
63      }
64      
65      public void alert(Duration duration, Severity severity, String message)
66      {
67          alert(duration, severity, message, false);
68      }
69  
70      public void alert(Duration duration, Severity severity, String message, boolean markup)
71      {
72          final Alert alert = new Alert(duration, severity, message, markup);
73  
74          if (request.isXHR())
75          {
76              addCallbackForAlert(alert);
77          }
78  
79          // Add it to the storage; this is always done, even in an Ajax request, because we may end up
80          // redirecting to a new page, rather than doing a partial page update on the current page ... in which
81          // case we need the alerts stored persistently until we render the new page.
82  
83          getAlertStorage().add(alert);
84      }
85  
86      private void addCallbackForAlert(final Alert alert)
87      {
88          ajaxResponseRenderer.addCallback(new JavaScriptCallback()
89          {
90              public void run(JavaScriptSupport javascriptSupport)
91              {
92                  javascriptSupport.require("t5/core/alert").with(alert.toJSON());
93              }
94          });
95  
96          addAlertStorageCleanupCallback();
97      }
98  
99      private void addAlertStorageCleanupCallback()
100     {
101         // Add a callback that exists just to clear the non-persistent alerts.
102         // Only one of these is needed.
103 
104         if (needAlertStorageCleanup.get(true))
105         {
106             ajaxResponseRenderer.addCallback(new JavaScriptCallback()
107             {
108                 public void run(JavaScriptSupport javascriptSupport)
109                 {
110                     // In an Ajax request, the Alerts are added, just so that they can be removed if not persistent.
111                     // Again, this is for the rare case where there's a redirect to another page.
112 
113                     getAlertStorage().dismissNonPersistent();
114                 }
115             });
116 
117             needAlertStorageCleanup.set(false);
118         }
119     }
120 
121     private AlertStorage getAlertStorage()
122     {
123         return asm.get(AlertStorage.class);
124     }
125 
126 }